package com.study.talk.module.service.impl;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.talk.module.entity.Message;
import com.study.talk.module.mapper.MessageMapper;
import com.study.talk.module.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author thor
 * @since 2023-12-18
 */
@Service
@Transactional
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {

    @Resource
    private MessageMapper messageMapper;

    private final Client client = new Client(new Config().setAccessKeyId("LTAI5tQPkVPi3ELZZ4F19b7A").setAccessKeySecret("TXEawwQdSezv0WTUtSA6X80GPNx1sp").setEndpoint("dysmsapi.aliyuncs.com"));

    public MessageServiceImpl() throws Exception {
    }

    @Override
    public void addMessage(Message message) {
        messageMapper.insert(message);
    }

    @Override
    public List<Message> getMessageList(Integer userId) {
        QueryWrapper<Message> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        return list(wrapper);
    }

    @Override
    public List<Message> getAllMessage(Integer userId) {
        return messageMapper.getMessageListByUserId(userId);
    }

    @Override
    public void deleteMessageList(Integer userId) {
        QueryWrapper<Message> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        remove(wrapper);
    }

    @Override
    public SendSmsResponse sendSms(String phoneNumber,String code) throws Exception {
        SendSmsRequest request = new SendSmsRequest()
                .setPhoneNumbers(phoneNumber)
                .setSignName("畅聊app")
                .setTemplateCode("SMS_301445151")
                .setTemplateParam("{ \"code\": \""+code+"\" }");

        return client.sendSms(request);
    }
}
